package com.sa.commons.web.mvc.controller;

import static com.sa.commons.web.mvc.constant.HttpPath.Suffix;

public abstract class ControllerSupport {

	protected static String Redirect = "redirect";

	protected static String Forward = "forward";

	private String httpPathPrefix = null;

	private String filePathPrefix = null;

	private String modulePath;
	private String modulePath_Short;
	private String subModulePath;
	private String subModulePath_Short;

	/**
	 * 
	 * @param modulePath
	 *            模块路径名
	 * @param modulePath_Short
	 *            模块路径名缩写
	 * @param subModulePath
	 *            子模块路径名
	 * @param subModulePath_Short
	 *            子模块路径名缩写
	 */
	public ControllerSupport(String modulePath, String modulePath_Short, String subModulePath,
			String subModulePath_Short) {
		this.modulePath = modulePath;
		this.modulePath_Short = modulePath_Short;
		this.subModulePath = subModulePath;
		this.subModulePath_Short = subModulePath_Short;

		httpPathPrefix = createHttpPathPrefix(modulePath, subModulePath_Short);
		filePathPrefix = createFilePathPrefix(modulePath, subModulePath);
	}

	/**
	 * 创建文件路径前缀
	 * 
	 * @param userPath
	 * @param modulePath
	 * @param subModulePath
	 * @return
	 */
	public static String createFilePathPrefix(String modulePath,String subModulePath) {
		return "/" + modulePath + "/" + subModulePath + "/";
	}

	/**
	 * 创建http请求前缀
	 * 
	 * @param userPath_Short
	 * @param subModulePath_Short
	 * @return
	 */
	public static String createHttpPathPrefix(String modulePath,
			String subModulePath_Short) {
		return "/" + modulePath + "/" + subModulePath_Short + "/";
	}

	/**
	 * 创建视图的名称
	 * 
	 * @param fileName
	 * @return
	 */
	public String createViewName(String fileName) {
		return filePathPrefix + fileName;
	}

	/**
	 * 创建本地重定向请求
	 * @param subHttpPath
	 * @param values
	 * @return
	 */
	public String createLocalRedirect(String subHttpPath,String[] values){
		return createHttpExchange(getHttpPathPrefix()+subHttpPath, values, Redirect);
	}
	
	/**
	 * 创建本地请求转发请求
	 * @param subHttpPath
	 * @param values
	 * @return
	 */
	public String createLocalForwardViewName(String subHttpPath,String[] values){
		return createHttpExchange(getHttpPathPrefix()+subHttpPath, values, Forward);
	}

	/**
	 * 创建转换链接
	 * @param httpPath
	 * @param values
	 * @param exchangeType
	 * @return
	 */
	public String createHttpExchange(String httpPath,String[] values,String exchangeType){
		
		if(values==null||values.length==0){
			return exchangeType+":"+httpPath+"."+Suffix;
		}
		StringBuffer buffer=new StringBuffer( exchangeType+":"+httpPath+"."+Suffix+"?");
		for(int i=0;i<values.length;i++){
			buffer.append(values[i]);
			if(i!=(values.length-1)){
				buffer.append("&");
			}
		}
		System.out.println("地址为:"+buffer.toString());
		return buffer.toString();
	}

	public String getHttpPathPrefix() {
		return httpPathPrefix;
	}

	public void setHttpPathPrefix(String httpPathPrefix) {
		this.httpPathPrefix = httpPathPrefix;
	}

	public String getFilePathPrefix() {
		return filePathPrefix;
	}

	public void setFilePathPrefix(String filePathPrefix) {
		this.filePathPrefix = filePathPrefix;
	}

	public String getModulePath() {
		return modulePath;
	}

	public void setModulePath(String modulePath) {
		this.modulePath = modulePath;
	}

	public String getModulePath_Short() {
		return modulePath_Short;
	}

	public void setModulePath_Short(String modulePathShort) {
		modulePath_Short = modulePathShort;
	}

	public String getSubModulePath() {
		return subModulePath;
	}

	public void setSubModulePath(String subModulePath) {
		this.subModulePath = subModulePath;
	}

	public String getSubModulePath_Short() {
		return subModulePath_Short;
	}

	public void setSubModulePath_Short(String subModulePathShort) {
		subModulePath_Short = subModulePathShort;
	}

}
